package org.esjsoftware.v2m.rim.view;

import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.text.TextFilter;

import org.esjsoftware.v2m.rim.common.Content;
import org.esjsoftware.v2m.rim.common.Messages;
import org.esjsoftware.v2m.rim.entity.ConfigurationEntity;
import org.esjsoftware.v2m.rim.model.ConfigurationModel;
import org.esjsoftware.v2m.rim.ui.FullWidthButton;
import org.esjsoftware.v2m.rim.util.datastorage.rms.RMSHandller;

/**
 * 
 * @author Ernest
 *
 */
public class ConfigurationScreen extends AbstractScreen {
	private static ConfigurationScreen instance;
	private LabelField vspAccountLabelField;
	private EditField userNameEditField;
	private PasswordEditField passwordEditField;
	private LabelField userParametersLabelField;
	private EditField senderPhoneNumberEditField;
	private LabelField toSubscribeLabelField;
	private FullWidthButton wwwFullButton;
	private boolean isSaved = false;
	
	//instance of Configuration Screen
	public static ConfigurationScreen getInstance(){
		if(instance == null) {
			instance = new ConfigurationScreen ();
		}
		return instance;
	}
	
	//constructor
	public ConfigurationScreen() {
		setTitle(Messages.getTITLE_CONFIGURE());
		vspAccountLabelField = new LabelField(Messages.getMSG_VSP_ACCOUNT()){
			public void paint(Graphics graphics) {
		        super.paint(graphics);
		    }
		};
		add(vspAccountLabelField);
        add(new SeparatorField());
        
        add(new LabelField(Messages.getMSG_USERNAME() + " (" + Messages.getMSG_VSP_USERNAME() + "): "));
        userNameEditField = new EditField("", "", Content.getMAX_PHONE_NUMBER_LENGTH() , (EditField.FILTER_LOWERCASE));
        ConfigurationModel cm = new ConfigurationModel();
        ConfigurationEntity ce = cm.getConfiguration();
        userNameEditField.setFilter(new TextFilter() {
            public char convert(char c, int i) {
                return c;  // don't convert anything here
            }
            public boolean validate(char c) {
                return (Character.isDigit(c) || Character.isLowerCase(c));
            }
        });
        
        userNameEditField.setText(ce.getUserName());

        add(userNameEditField);
        
        add(new LabelField(Messages.getMSG_PASSWORD() + " (" + Messages.getMSG_VSP_PASSWORD() + "): "));
        passwordEditField = new PasswordEditField("", "");
        passwordEditField.setText(ce.getPassword());
        add(passwordEditField);
        
        userParametersLabelField = new LabelField(Messages.getMSG_USER_PARAMETERS()){
			public void paint(Graphics graphics)
		    {
		        super.paint(graphics);
		    }
		};
        add(userParametersLabelField);
        add(new SeparatorField());
        

        add(new LabelField(Messages.getMSG_SENDER() + "(" + Messages.getMSG_SENDER_PHONE_NUMBER() + "): "));
        senderPhoneNumberEditField = new EditField("", "", Content.getMAX_PHONE_NUMBER_LENGTH() , EditField.FILTER_PHONE);
        senderPhoneNumberEditField.setText(ce.getSender());
        add(senderPhoneNumberEditField);
        
        toSubscribeLabelField = new LabelField(Messages.getMSG_TO_SUBSCRIBE()){
			public void paint(Graphics graphics)
		    {
		        super.paint(graphics);
		    }
		};
        add(toSubscribeLabelField);
        add(new SeparatorField());
        
        wwwFullButton = new FullWidthButton(Messages.getMSG_HTTP_VOICE2MOBILES(), Field.FOCUSABLE);
        wwwFullButton.setChangeListener(new FieldChangeListener(){
        	public void fieldChanged(Field field, int context) {
	           String websiteUrl = Messages.getMSG_HTTP_VOICE2MOBILES();
	           BrowserSession visit = Browser.getDefaultSession();
	           visit.displayPage(websiteUrl);
        	}
        });
        
        add(wwwFullButton);
        
        this.addMenuItem(_saveMenuItem);
        
		RMSHandller.Save(Content.getIS_FIRST_LOGIN_KEY(), Content.getIS_FIRST_LOGIN_VAR());
	}
	
	//regist save menu item listener
    private MenuItem _saveMenuItem = new MenuItem(Messages.getMSG_SAVE_BUTTON() , 0, 0) {
        public void run() {
        	String[] buttons = new String[] {Messages.getMSG_SAVE_BUTTON(), Messages.getMSG_CANCEL_BUTTON()};
    		if(Dialog.ask(Messages.getMSG_CONFIRM_SAVE_MESSAGE(), buttons, 0) == 0) {
    			saveConfiguration();
    		}
        }
    };
    
    //save configuration data
    public void saveConfiguration(){
    	ConfigurationModel cm = new ConfigurationModel();
    	cm.saveConfiguration(userNameEditField.getText(), passwordEditField.getText(), senderPhoneNumberEditField.getText());
    	isSaved = true;
    }
    
    public boolean onSave(){
    	saveConfiguration();
    	return true;
    }
    
    /**
     * Prevents the save dialog from being displayed
     * 
     * @see net.rim.device.api.ui.container.MainScreen#onSavePrompt()
     */
    public boolean onSavePrompt()
    {
        return true;
    }
}
